package com.yy.job.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.service.impl.YYJobStepServiceImpl;
import com.yy.job.core.domain.JobStepDomain;

@Service
public class JobStepDomainService {
	
	@Autowired
	private YYJobStepServiceImpl yyJobStepServiceImpl;
	
	/**
	 * 查询步骤列表，并根据stepOrder升序排序
	 * 
	 * @param jobId
	 * @return
	 */
	public List<JobStepDomain> listJobStepByJobId(Long jobId) {
		List<YYJobStepDO> taskDOList = yyJobStepServiceImpl.selectStepsByJobId(jobId);

		List<JobStepDomain> taskList = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(taskDOList)) {
			for (YYJobStepDO taskDO : taskDOList) {
				taskList.add(new JobStepDomain(taskDO));
			}

			// 排序
			Collections.sort(taskList, new Comparator<JobStepDomain>() {
				public int compare(JobStepDomain task1, JobStepDomain task2) {
					if (task1.getStepOrder() == null && task2.getStepOrder() == null)
						return 0;
					if (task1.getStepOrder() == null)
						return 1;
					if (task2.getStepOrder() == null)
						return -1;
					return task1.getStepOrder().compareTo(task2.getStepOrder());
				}
			});
		}

		return taskList;
	}
	
}
